import {
  Book,
  BookInterfaceWithDerive,
  PageData,
  getBookMapStatMonthDto,
  BookQueryCache,
  BookMapGateway,
  getBookMapListDto,
  User,
  getBookContactListDto,
  BookContactMapGateway,
  BookContactMap,
  BookDefaultGateway,
} from "qqlx-core";

import { ViewBase, ViewBaseAction } from "@view/view-base";
import { BookSchema } from "@/pond/schema-book";

export class ViewBook extends ViewBase<Book> implements ViewBaseAction<Book>, BookInterfaceWithDerive<{}> {
  private static instance: ViewBook;
  static getInstance(options?: { isSingleton?: boolean }) {
    if (options?.isSingleton === false) {
      return new ViewBook();
    }

    if (!this.instance) this.instance = new ViewBook();
    return this.instance;
  }

  private constructor() {
    super();
    this.initial();
    this.insertDefaultCondition();
  }

  getSchema(): Book {
    return new BookSchema();
  }

  async getTask() {
    this.pending();
    const path = `${BookDefaultGateway.path}/task`;
    return this.$request.getByPut<null, { count: number }>(path);
  }

  async getBookMapStatMonth(dto: getBookMapStatMonthDto) {
    this.pending();
    const path = `${BookMapGateway.path}/stat/month`;
    return this.$request.getByPut<getBookMapStatMonthDto, BookQueryCache>(path, dto);
  }

  async getBookMapList(dto: getBookMapListDto) {
    this.pending();
    const path = `${BookMapGateway.path}/list`;
    return this.$request.getByPut<getBookMapListDto, (BookQueryCache & { justCreator: User })[]>(path, dto);
  }

  async getBookContactList(dto: getBookContactListDto) {
    this.pending();
    const path = `${BookContactMapGateway.path}/list`;
    return this.$request.getByPut<getBookContactListDto, PageData<BookContactMap>>(path, dto);
  }
}
